﻿using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Core.Domain;
using QDT.Services.AdminModule;
using QDT.Common.Data;
using QDT.Web.Admin.Models;
using QDT.MVC.Security;

namespace QDT.Web.Admin.Controllers
{
    [SecurityModule(Name = "管理员")]
    public class AdminController : BaseController
    {
        protected IAdminService AdminService;

        public AdminController(IAdminService adminService)
        {
            Check.Argument.IsNotNull(adminService, "adminService");

            AdminService = adminService;
        }

        [SecurityNode(Name = "管理员列表")]
        public ActionResult Index(int? page, GridSortOptions sort)
        {
            const int pageSize = 20;
            var pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;
            var source = DbProvider.D<AdminUser>().Where(x => !x.IsDelete).OrderBy(sort.Column, sort.Direction == SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "管理员回收站")]
        public ActionResult Trash(int? page, GridSortOptions sort)
        {
            const int pageSize = 20;
            var pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;
            var source = DbProvider.D<AdminUser>().Where(x => x.IsDelete).OrderBy(sort.Column, sort.Direction == SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "添加管理员页")]
        public ActionResult Add()
        {
            ViewData["RoleSet"] = AdminService.GetRoles();
            return View();
        }

        [HttpPost]
        [SecurityNode(Name = "添加管理员操作")]
        public ActionResult PostAdd(AdminModel model)
        {
            if (!ModelState.IsValid) return Error();

            if (model.RoleId <= 0) return Error("请选择一个有效的管理员组");

            if (AdminService.ExistAdmin(model.Name)) return Error("管理员账户名已经存在");


            AdminService.CreateAdmin(model.ToEntity());
            return Success("添加成功");

        }

        [SecurityNode(Name = "编辑管理员页")]
        public ActionResult Edit(int id)
        {
            ViewData["RoleSet"] = AdminService.GetRoles();
            var admin = DbProvider.D<AdminUser>().FirstOrDefault(x => x.ID == id);
            return admin == null ? Error("管理员不存在或者已经被删除") : View(admin);
        }

        [HttpPost]
        [SecurityNode(Name = "编辑管理员操作")]
        public ActionResult PostEdit(EditAdminModel model)
        {
            if (!ModelState.IsValid) return Error();

            if (model.RoleId <= 0) return Error("请选择一个有效的管理员组");

            var admin = DbProvider.D<AdminUser>().FirstOrDefault(x => x.ID == model.ID);

            if (admin == null) return Error("管理员不存在或者已经被删除");

            admin.IsEffect = model.IsEffect;
            admin.RoleID = model.RoleId;

            if (!string.IsNullOrWhiteSpace(model.Password)) admin.ChangePwd(model.Password);

            AdminService.UpdateEntity(admin);

            return Success("修改成功");

        }

        [HttpPost]
        [SecurityNode(Name = "删除执行")]
        public ActionResult Delete(int[] id)
        {
            AdminService.DelAdmin(id);
            return Success("删除成功");
        }

        [HttpPost]
        [SecurityNode(Name = "永久删除")]
        public ActionResult ForeverDel(int[] id)
        {
            AdminService.ForeverDelAdmin(id);
            return Success("删除成功");
        }

        [HttpPost]
        [SecurityNode(Name = "恢复")]
        public ActionResult Restore(int[] id)
        {
            AdminService.RestoreAdmin(id);
            return Success("恢复成功");
        }
    }
}
